package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.bean.DishFlavor;
import com.itheima.common.R;
import com.itheima.dao.DishFlavorDao;
import com.itheima.dto.DishDto;
import com.itheima.exception.CustomException;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorDao dishFlavorDao;

    /**
     * 菜品新增
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        //1.调用service处理
        dishService.saveWithFlavors(dishDto);

        //2.响应处理结果
        return R.success("新增成功！");
    }

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(@RequestParam(defaultValue="1") Integer page, @RequestParam(defaultValue="10") Integer pageSize, String name){
        //1.调用service处理
        Page<DishDto> dishPage = dishService.findPage(page,pageSize,name);

        //2.响应处理结果
        return R.success(dishPage);
    }

    /**
     * 根据id查询菜品信息   /dish+/{id}
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        //1.调用service处理
        DishDto dishDto = dishService.getByIdWithFlavors(id);

        //2.响应处理结果
        return R.success(dishDto);
    }

    /**
     * 菜品修改
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        //1.调用service处理
        dishService.updateWithFlavors(dishDto);

        //2.响应处理结果
        return R.success("修改成功！");
    }

    /**
     * 根据分类id查询指定分类下的菜品列表数据
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId,String name,Integer status){
        //1.调用service处理  根据菜品分类id查询得到菜品列表数据
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        wrapper.like(name!=null&&name.length()>0,Dish::getName,name);
        wrapper.eq(status!=null,Dish::getStatus,status);

        //1.1:查询出菜品的基本信息列表
        List<Dish> list = dishService.list(wrapper);

        //1.2：根据每条菜品的id查询出菜品的口味信息 封装到DishDto对象中，将DishDto对象封装到List<DishDto>集合dishDtoList中
        List<DishDto> dishDtoList = list.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);

            //根据菜品id查询菜品的口味信息 封装到dishDto的flavors属性中
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> flavors = dishFlavorDao.selectList(wrapper1);
            dishDto.setFlavors(flavors);

            return dishDto;
        }).collect(Collectors.toList());

        //2.响应处理结果
        return R.success(dishDtoList);
    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 菜品状态修改
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids,@PathVariable Integer status){
        if(ids==null || ids.size()==0){
            throw new CustomException(0,"没有选中要修改的菜品!");
        }

        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Dish::getStatus,status).in(Dish::getId,ids);
        boolean flag = dishService.update(wrapper);
        return flag?R.success("菜品状态更改成功！"): R.error("菜品状态更改失败");
    }
}
